local jiyuan = fk.CreateSkill {
  name = "wk_heg__jiyuan",
}

Fk:loadTranslationTable{
  ["wk_heg__jiyuan"] = "急援",
  [":wk_heg__jiyuan"] = "与你势力相同的角色进入濒死状态时，你可以选择一项：1.令其摸一张牌；2.交给其一张牌，若此牌为红桃，其可以将此牌当【桃】使用。",

  ["wk_heg__jiyuan_draw"] = "令濒死角色摸牌",
  ["wk_heg__jiyuan_give"] = "交给濒死角色牌",
  ["wk_heg__jiyuan_use"] = "将此牌当【桃】使用",

  ["$wk_heg__jiyuan1"] = "公若此，必遭蔡瑁之害矣。",
  ["$wk_heg__jiyuan2"] = "情势危急，还请速行。",
}

local H = require "packages/ol_hegemony/util"

jiyuan:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(jiyuan.name) and H.compareKingdomWith(player, target)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choices = {"wk_heg__jiyuan_draw"}
    if #player:getCardIds("he") > 0 and player ~= target then
      table.insert(choices, "wk_heg__jiyuan_give")
    end
    local choice = room:askToChoice(player, {choices = choices, skill_name = jiyuan.name})
    local card 
    if choice == "wk_heg__jiyuan_give" then
      card = room:askToChooseCards(player,{
        target = player,
        min = 1,
        max = 1,
        flag = "he",
        skill_name = jiyuan.name,
      })
      room:obtainCard(target, card, false, fk.ReasonGive)
      if Fk:getCardById(card[1]).suit == Card.Heart and target:canUseTo(Fk:cloneCard("peach"), target) then
        local choices2 = {"wk_heg__jiyuan_use", "Cancel"}
        local choice2 = room:askToChoice(target, {choices = choices2, skill_name = jiyuan.name})
        if choice2 == "wk_heg__jiyuan_use" then
          room:useVirtualCard("peach", card, target, target, jiyuan.name, true)
        end
      end
    else
      target:drawCards(1, jiyuan.name)
    end
  end,
})

return jiyuan